package com.gidgabs.modules.base.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.gidgabs.common.utils.R;
import com.gidgabs.dto.TbImgEntityDto;
import com.gidgabs.modules.base.service.TbImgService;
import com.gidgabs.modules.entity.TbImgEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 图片表
 * 
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-06-07 18:07:05
 */
@RestController
@RequestMapping("/loca/img")
public class TbImgController {
	@Autowired
	private TbImgService imgService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(TbImgEntity tbImgEntity){

		//默认查询没有上级的图片 也就是最外层的图片
		tbImgEntity.setFirstImgId(0);
		if(tbImgEntity.getImgType() == null){
			return R.error("图片类型不能为空");
		}

		List<TbImgEntity> imgList = imgService.queryByList(tbImgEntity);
		//图片数据集
		List<TbImgEntityDto> tbImgEntityDtoList = new ArrayList<>();
		//判断查询的图片是否是log图和首页图
		//是 就不循环查询下级图片
		if(tbImgEntity.getImgType()== 1 || tbImgEntity.getImgType() == 2 ){
			for (int i = 0; i < imgList.size() ; i++) {
				TbImgEntityDto tbImgEntityDto = new TbImgEntityDto();
				tbImgEntityDto.setTbImgEntity(imgList.get(i));
				tbImgEntityDtoList.add(tbImgEntityDto);
			}
		}else{
			for (int i = 0; i < imgList.size() ; i++) {

				TbImgEntity tbImg = imgList.get(i);
				TbImgEntity tbImgEntityTwo = new TbImgEntity();
				tbImgEntityTwo.setFirstImgId(tbImg.getId());
				//查询下级图片
				List<TbImgEntity> imgListTwo = imgService.queryByList(tbImgEntityTwo);
				TbImgEntityDto tbImgEntityDto = new TbImgEntityDto();
				//要是没有下级图片
				if(imgListTwo == null || imgListTwo.size() == 0){
					//封装当前对象
					tbImgEntityDto.setTbImgEntity(tbImg);
				}else{
					//封装当前对象
					tbImgEntityDto.setTbImgEntity(tbImg);
					tbImgEntityDto.setTbImgEntityList(imgListTwo);
				}
				tbImgEntityDtoList.add(tbImgEntityDto);
			}
		}
		return R.ok().put("data", tbImgEntityDtoList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		TbImgEntity img = imgService.queryObject(id);
		
		return R.ok().put("img", img);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TbImgEntity img){
		imgService.save(img);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TbImgEntity img){
		imgService.update(img);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] ids){
		imgService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
